Page({



  /**
   * 页面的初始数据
   */
  data: {
    width: 0,
    height: 0,
    rainCount: 100,
    rain: {},
    rains: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //获取系统信息  
    wx.getSystemInfo({
      //获取系统信息成功，将系统窗口的宽高赋给页面的宽高  
      success: function (res) {
        that.width = res.windowWidth
        // console.log(that.width)   375
        that.height = res.windowHeight
        // console.log(that.height)  625
        // 这里的单位是PX，实际的手机屏幕有一个Dpr，这里选择iphone，默认Dpr是2
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.info(this.width);
    console.info(this.height);
    this.drawRedPakege();
    this.playRains();

    // var context = wx.createCanvasContext('redPakege');
    // context.translate(0, 0);
    // context.drawImage("../../images/ic_home_1_checked.png", 100, 100, 40, 40);
    // context.draw();

  },

  drawRedPakege: function () {

    var that = this;

    var context = wx.createCanvasContext('redPakege', this);
    var x = -1;
    var y = -1;
    var length = -1;//长度
    var angle = 90; //倾斜角度
    var width = -1;//宽度
    var height = -1;//高度
    var speed = 1;//速度
    var offset_x = 0;//横轴移动偏移量
    var offset_y = 0;//纵轴移动偏移量
    var alpha = 1; //透明度
    var color1 = "";//红包的色彩
    var color2 = "";  //红包的色彩

    /****************初始化函数********************/
    function init() { //初始化
      console.info("init");
      getPos();
      alpha = 1;//透明度
      // this.getRandomColor();
      //最小长度，最大长度
      var x = Math.random() * 80 + 150;
      length = Math.ceil(x);
      angle = Math.random() * 50 + 75; //红包倾斜角
      x = Math.random() + .4;
      speed = x; //红包的速度
      var cos = Math.cos(angle * 3.14 / 180);
      var sin = Math.sin(angle * 3.14 / 180) * 5;
      width = length * cos;  //红包所占宽度
      height = length * sin;//红包所占高度
      offset_x = speed * cos;
      offset_y = speed * sin;
    }

    /***************重新计算红包坐标的函数******************/
    function countPos() {
      //往左下移动,x减少，y增加
      x = x - offset_x;
      y = y + offset_y;
    }

    /*****************获取随机坐标的函数*****************/
    function getPos() {
      //横坐标200--1200
      console.info(that.width);
      console.info(that.height);
      x = Math.random() * that.width; //窗口宽度
      //纵坐标小于600
      y = Math.random() * that.height;  //窗口高度

      console.info("x" + x);
      console.info('y:' + y);
    }

    /****绘制红包***************************/
    function draw() { //绘制一个红包的函数
      console.info("x:" + x + " +  width: " + width + " = " + (x + width));
      console.info("y:" + y + " -  height: " + height + " = " + (y - height + 500));
      context.drawImage("../../images/ic_home_1_checked.png", x + width, y - height + 500, 59, 77);
      context.draw();
    }

    function move() {
      //清空红包像素
      var x = x + width - offset_x;
      var y = y - height;
      //重新计算位置，往左下移动
      countPos();
      //透明度增加
      alpha -= 0.002;
      //重绘
      draw();
    }

    // module.exports.draw = draw
    // exports.move = move
    // exports.init = init


    //画红包
    for (var i = 0; i < that.rainCount; i++) {
      var rain = this;
      rain.init();
      rain.draw();
      that.rains.push(rain);
    }


    // function playRains() {
    //   for (var n = 0; n < 100; n++) {
    //     move();//移动
    //     if (y > that.height * 3) {//超出界限后重来
    //       init();
    //     }
    //   }
    //   if (that.canvasShow == true) {
    //     var redBagTimer = setTimeout(function () {
    //       playRains()
    //     }, 2);
    //   } else {
    //     $("#stars").fadeOut(0);
    //     clearTimeout(redBagTimer)
    //   }
    // }


  },

  playRains: function () {
    for (var n = 0; n < this.rainCount; n++) {
      var rain = rains[n];
      rain.move();//移动
      // if (y > that.height * 3) {//超出界限后重来
      //   init();
      // }
    }
    if (this.canvasShow == true) {
      var redBagTimer = setTimeout(function () {
        playRains()
      }, 2);
    } else {
      $("#stars").fadeOut(0);
      clearTimeout(redBagTimer)
    }
  },

})